<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-01-04 15:43:15
 * @LastEditors: sunkaiyuan sunkaiyuan@namenode.com
 * @Description: 
 */

namespace App\Http\Services\System;


use App\Http\Services\Service;
use App\Repositories\ApiRepository;
use App\Repositories\SubscribeRepository;
use App\Repositories\SubscribeApiRepository;
use Illuminate\Support\Facades\Hash;

class SubscribeService extends Service
{

    public $api;
    public $SubApi;
    public function __construct(SubscribeRepository $repository, ApiRepository $api, SubscribeApiRepository $SubApi)
    {
        $this->repository = $repository;
        $this->api = $api;
        $this->SubApi = $SubApi;
    }

    public function list()
    {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
    }
    public function info()
    {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
    }

    public function listApi()
    {
        $res = request();
        $apiIds = [];
        $subscribe_id = $this->repository->getConditionFilter("subscribe_id", true);
        if ($subscribe_id) {
            $subdata = $this->repository->whereId($subscribe_id)->first();

            $apiIds = $subdata->subscribe_api->pluck("api_id")->toArray();
        } else {
            $this->api->whereId(0);
        }
        $data = $this->api->condition($res)->whereCan_subscribe(1)->getGrid();

        $data['rows']->map(function ($row) use ($apiIds, $subscribe_id) {
            if (in_array($row->id, $apiIds)) {
                $row['subscribe'] = '已订阅';
            } else
                $row['subscribe'] = '未订阅';
            $row['subscribe_id'] = $subscribe_id;
            return $row;
        });
        return $data;
    }

    protected function subscribe()
    {
        $request = request();
        $subscribe_id = $request->input('subscribe_id');
        $ids = $request->input('ids');
        foreach ($ids as $v) {
            $this->SubApi->updateOrCreate(['subscribe_id' => $subscribe_id, 'api_id' => $v]);
        }
        return $request->toArray();
    }

    protected function nosubscribe()
    {
        $request = request();
        $subscribe_id = $request->input('subscribe_id');
        $ids = $request->input('ids');
        foreach ($ids as $v) {
            $this->SubApi->whereSubscribe_id($subscribe_id)->whereApi_id($v)->delete();
        }
        return $request->toArray();
    }


    protected function encode()
    {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($row) {
            $row->key = Hash::make(time() . $row->url);
            return $row->save();
        });
    }
    protected function discode()
    {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($row) {
            $row->key = "";
            return $row->save();
        });
    }
    protected function disable()
    {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($row) {
            $row->status_id = -1;
            return $row->save();
        });
    }
    protected function enable()
    {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($row) {
            $row->status_id = 0;
            return $row->save();
        });
    }
    protected function save()
    {
        return $this->repository->autosave(request()->toArray());
    }
    protected function delete()
    {
        return $this->repository->whereIn("id", $this->getRequestIds())->with(['subscribe_api'])->autodelete();
    }
}